﻿using Newtonsoft.Json.Linq;

namespace RuoVea.Universal.Server.Util;

/// <summary>
/// JObjectExtend
/// </summary>
public static class JObjectExtend
{
    #region Value
    /// <summary>
    /// 给JObject对象赋值
    /// </summary>
    /// <param name="jobject"></param>
    /// <param name="ks">^隔开的多维数组索引</param>
    /// <param name="v">要赋的值</param>
    /// <returns></returns>
    public static bool SetValue(this JObject jobject, string ks, string v)
    {
        try
        {
            var k = ks.Split(':'); //k是一个^分割的多为数组索引的字符串，比如"1^2^3^4^5"
            object A = null; //先设定个中间存储万能值object
            for (var i = 0; i < k.Length - 1; i++)
            {
                A = i == 0 ? jobject[k[i]] : ((JObject)A)?[k[i]];
            }
            //对最终结果赋值
            if (A != null) ((JObject)A)[k[k.Length - 1]] = v;
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return false;
    }

    /// <summary>
    /// 根据传递的Key获取JObject中的值
    /// </summary>
    /// <param name="jobject"></param>
    /// <param name="ks">^隔开的多维数组索引</param>
    /// <returns></returns>
    public static (bool code, object value) GetValue(this JObject jobject, string ks)
    {
        object v;
        try
        {

            var k = ks.Split(':'); //k是一个^分割的多为数组索引的字符串，比如"1^2^3^4^5"
            object A = null; //先设定个中间存储万能值object
            for (var i = 0; i < k.Length - 1; i++)
            {
                A = i == 0 ? jobject[k[i]] : ((JObject)A)?[k[i]];
            }
            //取值
            v = ((JObject)A)?[k[k.Length - 1]].ToString();
            return (true, v);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        v = null;
        return (false, v);
    }
    #endregion

    #region ValueDynamic
    /// <summary>
    /// 根据传递的Key获取JObject中的值
    /// </summary>
    /// <param name="jobject"></param>
    /// <param name="ks">^隔开的多维数组索引</param>
    /// <returns></returns>
    public static (bool code, object value) GetValueDynamic(this JObject jobject, string ks)
    {
        object v;
        try
        {
            var k = ks.Split(':'); //k是一个^分割的多为数组索引的字符串，比如"1^2^3^4^5"
            dynamic A = null; //先设定个中间存储dynamic 动态类型
            for (var i = 0; i < k.Length - 1; i++)
            {
                A = i == 0 ? jobject[k[i]] : A?[k[i]];
            }
            //取值
            v = A?[k[k.Length - 1]].ToString();
            return (true, v);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        v = "";
        return (false, v);
    }

    public static bool SetValueDynamic(this JObject jobject, string ks, string v)
    {
        try
        {
            var k = ks.Split(':'); //k是一个^分割的多为数组索引的字符串，比如"1^2^3^4^5"
            dynamic A = null; //先设定个中间存储万能值object
            for (var i = 0; i < k.Length - 1; i++)
            {
                A = i == 0 ? jobject[k[i]] : A?[k[i]];
            }
            //对最终结果赋值
            if (A != null) A[k[k.Length - 1]] = v;
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return false;
    }
    #endregion
}
